package CS;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;

    public class T{
        public static void main(String[] args) {
            JFrame frame = new JFrame("羊了个羊");
            JPanel panel = new JPanel(null);
            JLabel background = new JLabel(new ImageIcon("images/背景.jpg"));
            frame.setIconImage(new ImageIcon("images/草.png").getImage());
            background.setSize(480,800);
            panel.add(background);
            frame.add(panel);
            frame.setSize(492, 842);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ArrayList<JButton> cards = createCards(9);
            //trough 槽子
            ArrayList<JButton> trough = new ArrayList<>();
            Collections.shuffle(cards);
            poseCards(panel, cards, 7, 7, 0, 30, 100);
            poseCards(panel, cards, 6, 6, 49, 30+30, 100+30);
            poseCards(panel, cards, 5, 7, 49+36, 30, 100+60);
            poseCards(panel, cards, 6, 6, 49+36+35, 30+30, 100+30+60);
            checkCovered(cards);
            addAction(cards, trough, panel);
            frame.setVisible(true);
        }

        private static void addAction(ArrayList<JButton> cards, ArrayList<JButton> trough,
                                      JPanel panel) {
            for(int i = 0; i< cards.size(); i++){
                JButton card = cards.get(i);
                card.addActionListener(e->{
                    //e 称为事件, 其中包含点击时候发生的数据, 鼠标位置, 点击时间
                    //被点击的当前对象, e.getSource() 方法返回被点击的对象,
                    //Source 来源, 是发生时间时候的当前对象
                    JButton current = (JButton) e.getSource();
                    System.out.println(current.getName());
                    System.out.println("OK");
                    if (trough.size() == 7){
                        // show 显示 Message 消息 Dialog 对话框
                        JOptionPane.showMessageDialog(panel, "槽子满了!");
                        return;
                    }
                    addToTrough(cards, trough, current);
                });
            }
        }
        /*
         * 将当前被点击的牌, 添加到槽子
         */
        private static void addToTrough(ArrayList<JButton> cards,
                                        ArrayList<JButton> trough,
                                        JButton current) {
            //从上方堆牌中, 删除当前被点击的牌
            cards.remove(current);
            //将删除的当前牌, 添加到下方槽子中
            checkCovered(cards);
            trough.add(current);
            for (int j = 0; j< trough.size(); j++){
                JButton c = trough.get(j);
                c.setLocation(20 + j * 63, 642);
            }
        }

        /**
         * 检查卡牌的遮挡关系
         * 检查每个牌,如果这个牌被后续牌遮挡, 这个牌就设置为 disable 状态
         * @param cards 被检查全部牌
         */
        public static void checkCovered(ArrayList<JButton> cards){
            for (int i=0; i<cards.size(); i++){
                //前面牌是下层牌c1
                JButton c1 = cards.get(i);
                c1.setEnabled(true);
                for (int j=i+1; j<cards.size(); j++){
                    //c2代表c1后面的每个牌
                    JButton c2 = cards.get(j);
                    if(covered(c1, c2)){
                        //如果c1被压住, 则c1设置为disable, 不用再检查后续是否压住
                        c1.setEnabled(false);
                        break;
                    }
                }
            }
        }

        /**
         * 检查, c2 是否压住了 c1
         * @param c1 是下方的卡牌
         * @param c2 是上方的卡牌
         * @return true 表示压住了, false 表示没有压上
         */
        public static boolean covered(JButton c1, JButton c2){
            int x1 = c1.getX() - 59;
            int x2 = c1.getX() + 59;
            int y1 = c1.getY() - 59;
            int y2 = c1.getY() + 59;
            return  (c2.getX()>x1 && c2.getX()<x2) &&
                    (c2.getY()>y1 && c2.getY()<y2);
        }
        public static ArrayList<JButton> createCards(int rounds){
            String[] names = {"刷子","剪刀","叉子","奶瓶","干草","手套","树桩",
                    "棉花","毛线","水桶","火","玉米","白菜","草","萝卜","铃铛"};
            ArrayList<JButton> cards = new ArrayList<>();
            for (int j=0; j<rounds; j++){
                for (int i=0; i<names.length; i++){
                    String name = names[i];
                    String filename = "images/" + name + ".png";
                    String filename2 = "images/" + name + "2.png";
                    JButton card = new JButton(new ImageIcon(filename));
                    card.setName(name);
                    card.setDisabledIcon(new ImageIcon(filename2));
                    card.setBorderPainted(false);
                    card.setContentAreaFilled(false);
                    card.setSize(59, 66);
                    cards.add(card);
                }
            }
            return cards;
        }
        /*
         * 摆牌
         */
        public static void poseCards(JPanel panel, ArrayList<JButton> cards,
                                     int rows, int cols,
                                     int offset,
                                     int x, int y){
            for (int i=0; i<rows*cols; i++){
                if (i+offset == cards.size()){
                    break;
                }
                JButton card = cards.get(i+offset);
                card.setLocation(x + i % cols * 59, y + i / cols * 59);
                panel.add(card,0);
            }
        }


}
